package com.hucheng.rest.base;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.hucheng.common.exception.EduException;
import com.hucheng.common.request.ServiceCoreQuery;
import com.hucheng.common.response.ServiceCoreVO;
import com.hucheng.common.utils.ContextUtils;
import com.hucheng.common.utils.StringUtils;
import com.hucheng.common.utils.ValidatorUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-06-26
 * @Description: 请求处理
 **/
@Component("baseAction")
public class BaseAction {

    /**
     * 全局请求action接口
     */
    protected String action;

    /**
     * accessId用户请求唯一标识
     */
    protected String accessId;

    /**
     * 用户id
     */
    protected Integer userId;

    /**
     * 请求参数
     */
    protected JSONObject reqObject;
    /**
     * 调用执行方法
     *
     * @param serviceCoreQuery
     * @return
     * @throws EduException
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public ServiceCoreVO execute(ServiceCoreQuery serviceCoreQuery) throws EduException {
        try {
            Object executeAction = ContextUtils.getBean(serviceCoreQuery.getBean());
            Class classObject = executeAction.getClass();
            Class baseClass = ContextUtils.getBean("baseAction").getClass();
            Method method0 = baseClass.getDeclaredMethod("init", ServiceCoreQuery.class);
            method0.invoke(executeAction, serviceCoreQuery);
            Method method = classObject.getDeclaredMethod(serviceCoreQuery.getMethod());
            return (ServiceCoreVO) method.invoke(executeAction);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getCause() != null && e.getCause().getMessage() != null) {
                String tp = e.getCause().getMessage();
                if (ValidatorUtils.isDigital(tp)) {
                    int code = Integer.valueOf(tp);
                    throw new EduException(code);
                }
                throw new EduException(tp);
            }
            if (e.toString().indexOf("NoSuchMethodException") != -1) {
                throw new EduException(110004);
            }
            throw new EduException(e.getMessage());
        }
    }

    /**
     * 初始化全局参数
     *
     * @param serviceCoreQuery
     */
    public void init(ServiceCoreQuery serviceCoreQuery) {
        this.action = serviceCoreQuery.getAction();
        this.reqObject = JSONObject.parseObject(JSONObject.toJSONString(serviceCoreQuery.getReqObject()));
        this.userId = serviceCoreQuery.getUserId();
        this.accessId = serviceCoreQuery.getAccessId();
    }

    /**
     * 返回空结果
     *
     * @return
     */
    protected ServiceCoreVO response() {
        return new ServiceCoreVO();
    }

    /**
     * 自定义消息返回结果
     *
     * @return
     */
    protected ServiceCoreVO response(int code) {
        return new ServiceCoreVO(code);
    }

    /**
     * 自定义消息返回结果
     *
     * @return
     */
    protected ServiceCoreVO response(String message) {
        return new ServiceCoreVO(message);
    }

    /**
     * 返回结果
     *
     * @return
     */
    protected ServiceCoreVO response(Object data) {
        return new ServiceCoreVO(data);
    }

    /**
     * 获取参数
     *
     * @param name 参数名
     * @return
     * @throws EduException
     */
    protected String getRequestContentValue(String name) throws EduException {
        return getRequestContentValue(this.reqObject, name);
    }

    /**
     * 获取参数
     *
     * @param content 参数主体
     * @param name    参数key
     * @return
     * @throws EduException
     */
    protected static String getRequestContentValue(Object content, String name) throws EduException {
        if (content == null) {
            throw new EduException(110003);
        }
        String value = "";
        if (content instanceof JSONObject) {
            try {
                value = StringUtils.getInstance().nullToStrTrim(String.valueOf(((JSONObject) content).get(name)));
            } catch (JSONException e) {
                value = "";
            }
        } else {
            throw new EduException(110003);
        }
        return value.trim();
    }

}
